package ftl.yyh.mall.coupon.controller;

import ftl.yyh.common.utils.PageUtils;
import ftl.yyh.common.utils.R;
import ftl.yyh.mall.coupon.entity.CouponEntity;
import ftl.yyh.mall.coupon.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 优惠券信息
 *
 * @author yyh
 * @email yangyyh@163.com
 * @date 2021-07-25 09:55:36
 */
@RestController
@RequestMapping("/coupon/coupon" )
@Api(tags = "优惠券信息" )
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 列表
     */
    @GetMapping("/pageList" )
    @ApiOperation(value = "高级查询带分页" )
    public R pageList(
            @ApiParam(name = "params", value = "查询条件page,limit", required = true)
            @RequestParam Map<String, Object> params) {
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list" )
    @ApiOperation(value = "查询所有" )
    public R list() {
        List<CouponEntity> list = couponService.list(null);

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}" )
    @ApiOperation(value = "根据id查询" )
    public R info(
            @ApiParam(name = "id", value = "编号", required = true)
            @PathVariable("id" ) Long id) {
            CouponEntity coupon = couponService.getById(id);

        return R.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @PostMapping("/save" )
    @ApiOperation(value = "添加" )
    public R save(
            @ApiParam(name = "coupon", value = "参数对象", required = true)
            @RequestBody CouponEntity coupon) {
            couponService.save(coupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update" )
    @ApiOperation(value = "修改" )
    public R update(
            @ApiParam(name = "coupon", value = "参数对象", required = true)
            @RequestBody CouponEntity coupon) {
            couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete" )
    @ApiOperation(value = "批量删除" )
    public R delete(@RequestBody Long[] ids) {
            couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
